<template>
   
  <div class="app-container">
    <el-card class="filter-container" shadow="never">
      <div>
        <i class="el-icon-search"></i>
        <span>筛选搜索</span>
        <el-button
          style="float: right"
          @click="searchSubjectCommentList()"
          type="primary"
          size="small"
        >
          查询结果
        </el-button>
      </div>
      <div style="margin-top: 15px">
        <el-form
          :inline="true"
          :model="listQuery"
          size="small"
          label-width="140px"
        >
          <el-form-item label="输入搜索：">
            <el-input
              style="width: 203px"
              v-model="listQuery.content"
              placeholder="评论内容"
            ></el-input>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
    <el-card class="operate-container" shadow="never">
      <i class="el-icon-tickets"></i>
      <span>数据列表</span>
    </el-card>
    <div class="table-container">
       <!-- @selection-change="handleSelectionChange" -->
      <el-table
        ref="subjectCommentTable"
        :data="list"
        style="width: 100%"
       
        v-loading="listLoading"
        border
      >
        <!-- <el-table-column
          type="selection"
          width="60"
          align="center"
        ></el-table-column> -->

        <el-table-column label="编号" align="center">
          <template slot-scope="scope">{{ scope.row.id }}</template>
        </el-table-column>
        <el-table-column label="科技馆" align="center" v-if="storeId==0">
          <template slot-scope="scope">{{ scope.row.storeName }}</template>
        </el-table-column>
        <el-table-column label="科普标题" align="center">
          <template slot-scope="scope">{{ scope.row.title }}</template>
        </el-table-column>
        <el-table-column label="用户名" align="center">
          <template slot-scope="scope">{{ scope.row.memberNickName }}</template>
        </el-table-column>
        <el-table-column label="用户图标" align="center">
          <template slot-scope="scope"
            ><img style="height: 80px" :src="scope.row.memberIcon"
          /></template>
        </el-table-column>
        <el-table-column label="状态" align="center">
          <template slot-scope="scope">
            <el-switch
              @change="handleStatusStatusChange(scope.$index, scope.row)"
              :active-value="0"
              :inactive-value="1"
              v-model="scope.row.showStatus"
            >
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column label="评论内容" align="center">
          <template slot-scope="scope">{{ scope.row.content }}</template>
        </el-table-column>
        <el-table-column label="评论时间" align="center">
          <template slot-scope="scope">{{
            scope.row.createTime | formatCreateTime
          }}</template>
        </el-table-column>
        <el-table-column label="回复内容" align="center">
          <template slot-scope="scope">{{ scope.row.replyContent }}</template>
        </el-table-column>
        <el-table-column label="回复时间" align="center">
          <template slot-scope="scope">{{
            scope.row.replyTime | formatCreateTime
          }}</template>
        </el-table-column>

        <el-table-column label="操作" align="center" fixed="right" width="170">
          <template slot-scope="scope">
            <el-button
              v-if="!scope.row.replyContent && storeId > 0"
              size="mini"
              type="primary"
              @click="handleReply(scope.$index, scope.row)"
              >回复
            </el-button>
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)"
              >删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="batch-operate-container"></div>
    <el-dialog title="回复评论" :visible.sync="dialog">
      <el-form v-model="form" ref="form">
        <el-form-item prop="replyContent">
          <el-input
            v-model="form.replyContent"
            placeholder="请输入评论内容"
            type="textarea"
            :rows="6"
          ></el-input>
        </el-form-item>
        <el-form-item style="text-align: right">
          <el-button @click="dialog = false">取 消</el-button>
          <el-button type="primary" @click="replyApi">确 定</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes,prev, pager, next,jumper"
        :page-size="listQuery.pageSize"
        :page-sizes="[10, 15, 50]"
        :current-page.sync="listQuery.pageNum"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>
<script>
import {
  fetchList,
  deleteSubjectComment,
  updateSubjectComment,
  updateShowOrHide,
} from "@/api/cms/subjectComment";
import { formatDate } from "@/utils/date";
export default {
  name: "subjectCommentList",
  data() {
    return {
      form: {
        replyContent: "",
        id: "",
      },
      dialog: false,
      operates: [
        {
          label: "显示品牌",
          value: "showSubjectComment",
        },
        {
          label: "隐藏品牌",
          value: "hideSubjectComment",
        },
      ],
      operateType: null,
      listQuery: {
        keyword: null,
        pageNum: 1,
        pageSize: 10,
      },
      list: null,
      total: null,
      listLoading: true,
      multipleSelection: [],
      storeId: null,
    };
  },
  created() {
    this.getList();
    this.storeId = this.$store.state.user.storeId;
  },
  filters: {
    formatCreateTime(time) {
        if(time){
             let date = new Date(time);
      return formatDate(date, "yyyy-MM-dd hh:mm:ss");
        }
     
    },

    formatStatus(value) {
      if (value === 1) {
        return "未开始";
      } else if (value === 2) {
        return "活动中";
      } else if (value === 3) {
        return "已结束";
      } else if (value === 4) {
        return "已失效";
      }
    },
  },
  methods: {
    // 科普评论的状态改变  0显示 1隐藏
    handleStatusStatusChange(index, row) {
      updateShowOrHide({id:row.id, showStatus:row.showStatus}).then((response) => {
        console.log(response)
      });
    },
    // 回复
    handleReply(index, row) {
      this.dialog = true;
      this.form.id = row.id;
    },
    replyApi() {
      if (this.form.replyContent == "") {
        this.$message.error("请输入评论内容");
        return;
      }
      updateSubjectComment(this.form.id, this.form).then((res) => {
        this.$message.success(res.data);
        this.dialog = false;
        this.getList();
      });
    },
    getList() {
      this.listLoading = true;
      fetchList(this.listQuery).then((response) => {
        this.listLoading = false;
        this.list = response.data.records;
        this.total = response.data.total;
        this.totalPage = response.data.pages;
        this.pageSize = response.data.size;
      });
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    handleUpdate(index, row) {
      this.$router.push({
        path: "/cms/updateSubjectComment",
        query: { id: row.id },
      });
    },
    handleDelete(index, row) {
      this.$confirm("是否要删除该评论", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        deleteSubjectComment(row.id).then((response) => {
          this.$message({
            message: "删除成功",
            type: "success",
            duration: 1000,
          });
          this.getList();
        });
      });
    },
    getProductList(index, row) {
      console.log(index, row);
    },
    getProductCommentList(index, row) {
      console.log(index, row);
    },

    handleSizeChange(val) {
      this.listQuery.pageNum = 1;
      this.listQuery.pageSize = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.listQuery.pageNum = val;
      this.getList();
    },
    searchSubjectCommentList() {
      this.listQuery.pageNum = 1;
      this.getList();
    },
    handleBatchOperate() {
      console.log(this.multipleSelection);
      if (this.multipleSelection < 1) {
        this.$message({
          message: "请选择一条记录",
          type: "warning",
          duration: 1000,
        });
        return;
      }
      let showStatus = 0;
      if (this.operateType === "showSubjectComment") {
        showStatus = 1;
      } else if (this.operateType === "hideSubjectComment") {
        showStatus = 0;
      } else {
        this.$message({
          message: "请选择批量操作类型",
          type: "warning",
          duration: 1000,
        });
        return;
      }
      let ids = [];
      for (let i = 0; i < this.multipleSelection.length; i++) {
        ids.push(this.multipleSelection[i].id);
      }
      let data = new URLSearchParams();
      data.append("ids", ids);
      data.append("showStatus", showStatus);
      updateShowStatus(data).then((response) => {
        this.getList();
        this.$message({
          message: "修改成功",
          type: "success",
          duration: 1000,
        });
      });
    },
    addSubjectComment() {
      this.$router.push({ path: "/cms/addSubjectComment" });
    },
  },
};
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
</style>


